package com.study.demo_resttemplate.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.study.demo_resttemplate.entity.interfaceentity.IntefaceApi;
import com.study.demo_resttemplate.entity.sdkentity.SDKApiKeyPair;
import com.study.demo_resttemplate.entity.sdkentity.SDKApiSign;
import com.study.demo_resttemplate.entity.sdkentity.SDKApiSignData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Name: RestTemplateTest
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2022/7/15 11:38
 * @Remark: 特殊说明
 * @Version: 1.0
 */
@Component
public class UserInteface {
    @Autowired
    @Qualifier("restTemplateWithInterceptors")
    RestTemplate restTemplate;

    @Autowired
    SDKInteface sdkInteface;

    private String API_BASE="https://open.zxchain.qq.com/";

    public IntefaceApi verify_code(Integer type, String cardNo, String name){
        Map<String,Object> request = new HashMap<>();
        request.put("type",type);
        request.put("cardNo",cardNo);
        request.put("name",name);
        return restTemplate.postForObject(API_BASE + "api/v1/user/query/verify_code", request, IntefaceApi.class);
    }

    public IntefaceApi query(Integer type, String cardNo, String name, String verifyCode){
        Map<String,Object> request = new HashMap<>();
        request.put("type",type);
        request.put("cardNo",cardNo);
        request.put("name",name);
        request.put("verifyCode",verifyCode);
        return restTemplate.postForObject(API_BASE + "api/v1/user/query", request, IntefaceApi.class);
    }

    public IntefaceApi upload_platform(MultipartFile file,Integer type, String cardNo, String name, String verifyCode){
        Map<String,Object> request = new HashMap<>();
        request.put("file",file);
        request.put("platformIdentification",cardNo);
        request.put("platformPubKey",name);
        request.put("platformSignData",verifyCode);
        return restTemplate.postForObject(API_BASE + "api/v1/user/business_license/upload_platform", request, IntefaceApi.class);
    }

    public IntefaceApi down_grade(String epName, String email, String creditCode, Integer busiLicenseId, Integer officialLetterId, String representativeName,
                                            String mobile, String idcard, Integer cardType, String platformName, String platformUrl, Integer businessType, String platformPubKey,
                                            String platformSignData){
        Map<String,Object> request = new HashMap<>();
        request.put("epName",epName);
        request.put("email",email);
        request.put("creditCode",creditCode);
        request.put("busiLicenseId",busiLicenseId);
        request.put("officialLetterId",officialLetterId);
        request.put("representativeName",representativeName);
        request.put("contact",representativeName);
        request.put("mobile",mobile);
        request.put("idcard",idcard);
        request.put("cardType",cardType);
        request.put("platformName",platformName);
        request.put("platformUrl",platformUrl);
        request.put("businessType",businessType);
        request.put("platformPubKey",platformPubKey);
        request.put("platformSignData",platformSignData);
        return restTemplate.postForObject(API_BASE + "api/v1/user/business_license/upload_platform", request, IntefaceApi.class);
    }

    public IntefaceApi submit_by_trusted_platform(String userPubKey, SDKApiKeyPair platformKey, String userIdentification){
        Map<String,Object> request = new HashMap<>();
        SDKApiSignData userSignData = sdkInteface.signByPriKey(userIdentification, platformKey.getPriKey());
        request.put("userSignData", userSignData);
        request.put("userPubKey",userPubKey);
        request.put("platformPubKey",platformKey.getPubKey());
        request.put("platformSignData",sdkInteface.signByPriKey(userSignData.getSignedData(),platformKey.getPriKey()));
        request.put("userIdentification",userIdentification);
        return restTemplate.postForObject(API_BASE + "api/v1/user/business_license/upload_platform", request, IntefaceApi.class);
    }

    public IntefaceApi bind_query(String[] addressList){
        String join = String.join("&addressList=", addressList);
        return restTemplate.getForObject(API_BASE + "api/v1/user/identity/bind/query?addressList="+join.toString(),IntefaceApi.class);
    }

    public IntefaceApi getUserInfo(){
        return restTemplate.getForObject(API_BASE + "api/v1/user/info/get",IntefaceApi.class);
    }

    public IntefaceApi person_down_grade(String personName, String email,
                                         String mobile, String idCard, Integer cardType, SDKApiKeyPair sdkApiKeyPair){
        Map<String,Object> request = new HashMap<>();
        request.put("personName",personName);
        request.put("email",email);
        request.put("mobile",mobile);
        request.put("idCard",idCard);
        request.put("cardType",cardType);
        request.put("platformPubKey",sdkApiKeyPair.getPubKey());
        request.put("platformSignData", sdkInteface.signByPriKey(sdkApiKeyPair.getPriKey(), personName +"_" + mobile +"_" + idCard).getSignedData());
        HttpEntity httpEntity=new HttpEntity(request);
        return restTemplate.postForObject(API_BASE + "api/v1/user/register/person_platform/down_grade", httpEntity, IntefaceApi.class);
    }
}
